package xyz.xtt.exchange.vo;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import org.hibernate.validator.constraints.Length;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.nacos.shaded.com.google.common.primitives.Longs;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.Data;
import lombok.NoArgsConstructor;
import xyz.xtt.asset.api.enums.AssetTypeEnum;
import xyz.xtt.exchange.entity.TblAssetAudit;

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class MintRequestExRequest {
	private Long auditId;
	@NotBlank(message = "nft名称为必填项")
	@Schema(description = "名称")
	@Length(max = 30, message = "名称超过最大长度")
	private String assetName;
	@Schema(description = "NFT展示图")
	@NotBlank(message = "NFT展示图为必填项")
	private String imagePath;
	@Schema(description = "视频地址")
	private String videoPath;
	@Schema(description = "原图地址")
	private String localImgPath;
//	private Long rightAmount;
	private Integer aiImgNum;
	@Min(value = 1, message = "数量下限为1")
	@Max(value = 9999, message = "数量上限为9999")
	@Schema(description = "数量")
	private Integer amount;
	@Default
	private Boolean upChain = Boolean.TRUE;
	@Schema(description = "描述")
	@NotBlank(message = "描述为必填项")
	@Length(max = 100, message = "描述超过最大长度")
	private String description;
	@Schema(description = "铸造token")
	@NotBlank(message = "铸造token")
	private String mintToken;
	@Schema(description = "风格id")
	@NotNull(message = "风格id不能是空")
	private Long cooperationId;
	@Schema(description = "宽")
	private String width;
	@Schema(description = "高")
	private String height;
	@Schema(description = "时长")
	private String duration;

	@Schema(description = "铸造类型")
	private Integer ownerType;

	@Schema(description = "权益包列表")
	private String packageIds;
	@Schema(description = "图片列表")
	private List<String> filePaths;
	@Schema(description = "资产分类，普通12/盲盒 61")
	private Integer firstType;

	public boolean checkVideo() {
		List<String> param = Arrays.asList(imagePath, videoPath, width, height, duration);
		boolean anyMatch = param.stream().allMatch(StringUtils::isNotBlank);
		return anyMatch;
	}

	public boolean checkImage() {
		return StringUtils.isNotBlank(imagePath) && StringUtils.isNotBlank(localImgPath);

	}

	public boolean isVideo() {
		return StringUtils.isNotBlank(imagePath) && StringUtils.isNotBlank(videoPath);
	}

	public Integer checkAuditType() {
		// 视频铸造
		if (StringUtils.isNotBlank(imagePath) && StringUtils.isNotBlank(videoPath)) {
			return 1;
		}
		// 盲盒
		if (Objects.nonNull(filePaths) && !filePaths.isEmpty()) {
			return 2;
		}
		return 0;
	}

	public TblAssetAudit buildTblAssetAudit(String uid, AssetTypeEnum tokenType, Integer rightAmount) {
		String filePathStr = Objects.isNull(filePaths) || filePaths.isEmpty() ? "" : JSONUtil.toJsonStr(filePaths);
		TblAssetAudit build = TblAssetAudit
				.builder()
				.id(IdUtil.getSnowflakeNextId())
				.assetName(assetName)
				.appUserId(Longs.tryParse(uid))
				.description(description)
				.collectionId(cooperationId)
				.firstType(tokenType.code())
				.secondType(0)
				.imgPath(imagePath)
				.videoPath(videoPath)
				.localImgPath(localImgPath)
				.aiImgNum(aiImgNum)
				.amount(amount)
				.ownerType(ownerType)
				.packageIds(packageIds)
				.rightAmount(rightAmount)
				.createTime(LocalDateTime.now())
				.updateTime(LocalDateTime.now())
				.fileList(filePathStr)
				.build();
		return build;
	}

//	public TblAssetExt buildGroundAssetExt(Long assetId, Long tokenId, String chainId, ReferProtocolEnum tokenType) {
//		return TblAssetExt
//				.builder()
//				.assetId(assetId)
//				.id(IdUtil.getSnowflakeNextId())
//				.tokenId(tokenId)
//				.chainName(" ")
//				.chainId(String.valueOf(chainId))
//				.referProtocol(tokenType.getDescription())
//				.sellStartDatetime(0L)
//				.sellEndDatetime(0L)
//				.lastModifyDatetime(System.currentTimeMillis())
//				.upChain(0)
//				.showType(0)
//				.build();
//	}

	public String checkText() {
		return String.format("%s$%s", assetName, description);
	}

	public Boolean checkUpChain() {
		return Objects.nonNull(upChain) && Objects.equals(Boolean.TRUE, upChain);
	}
}
